import java.util.concurrent.ArrayBlockingQueue;

/**
 * 等待唤醒机制【阻塞队列方法】
 */
public class WaitWakeUpQueue {

    private static ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(1);

    public static void main(String[] args) {
        Cook c = new Cook(arrayBlockingQueue);
        Food f = new Food(arrayBlockingQueue);
        c.setName("厨师");
        f.setName("吃货");
        c.start();
        f.start();

    }

    static class Food extends Thread{

        private static ArrayBlockingQueue<String> arrayBlockingQueue;

        public Food(ArrayBlockingQueue<String> arrayBlockingQueue) {
            Food.arrayBlockingQueue = arrayBlockingQueue;
        }

        @Override
        public void run() {
            while(true){
                try {
                    String take = arrayBlockingQueue.take();
                    System.out.println("吃货吃了" + take);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Cook extends Thread{

        private static ArrayBlockingQueue<String> arrayBlockingQueue;

        public Cook(ArrayBlockingQueue<String> arrayBlockingQueue) {
            Cook.arrayBlockingQueue = arrayBlockingQueue;
        }

        @Override
        public void run() {
            while(true){
                try {
                    arrayBlockingQueue.put("面条");
                    System.out.println("厨师放一碗面");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
